<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;
use app\admin\model\Community;
use app\admin\model\Form1;
use app\admin\model\TemperatureAlarm;

/**
 * @ControllerAnnotation(title="报警记录")
 */
class Alarm extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\PositiveAlarm();

        $this->assign('getCheckResultList', (new Form1)->getCheckResultList());

        $this->assign('getCommunityList', Community::getCommunityList());
    }

    /**
     * @NodeAnotation(title="阳性报警")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            // 筛选阳性
            $where[] = ['check_result', '=', 1];

            // 社区数据筛选
            if (!$this->isSuper) {
                $where[] = ['community', '=', $this->user->community_id];
            }

            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="体温报警")
     */
    public function temperature_index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            // 社区数据筛选
            if (!$this->isSuper) {
                $where[] = ['community', '=', $this->user->community_id];
            }

            $count = TemperatureAlarm::where($where)->count();

            $list = TemperatureAlarm::where($where)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();

            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
}